package cn.jsc.org.service.impl;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.service.impl.BaseServiceImpl;
import cn.jsc.org.domain.Employee;
import cn.jsc.org.domain.Shop;
import cn.jsc.org.mapper.EmployeeMapper;
import cn.jsc.org.mapper.ShopMapper;
import cn.jsc.org.service.IEmployeeServiceI;
import cn.jsc.org.service.IShopServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 店铺服务层
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopServiceI {
    //service也可以注入service - 为了调用添加t_logininfo的信息
    @Autowired
    private IEmployeeServiceI employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;
    @Override
    public void settlement(Shop shop) {
        //1.校验
        //1.1.空值校验 - 略
        //1.2.校验是否注册过
        Employee employee = employeeMapper.loadByAccount(shop.getAdmin());
        if(employee != null){
            //入驻过
            throw new BusinessException("您已经入驻过，请直接登录！");
        }
        //2.入驻 = 添加
        //2.1.添加员工 - 返回自增id
        Employee admin = shop.getAdmin();

        employeeService.add(admin);//调用service  - 既加了t_employee的信息又加了t_logininfo的信息
//      employeeMapper.save(admin);//admin的id有值 - 调用mapper - 只能添加t_employee的信息
        //2.2.添加店铺 - 返回自增id
        //给店铺的admin_id设置值
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);//shop的id就有值
        System.out.println(shop);
        //2.3.更新员工表t_employee中的shop_id
        //shop{ id=有值，admin{id}有值 }
        employeeMapper.updateShopId(shop);
    }
}
